using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace QLNT_DAO
{
    public class MyExecute
    {

        ////////////////////////Thuc thi procedure select/////////////////////////////////
        public static DataTable ExecuteQuery(String spName, List<SqlParameter> listpara)
        {
            DataTable dt = new DataTable();
            try
            {
                SqlConnection con = Connect_DB.ConnectionData();
                con.Open();
                SqlCommand command = con.CreateCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = spName;
                if (spName != null)
                {
                    foreach (SqlParameter para in listpara)
                    {
                        command.Parameters.Add(para);
                    }
                }
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                adapter.Fill(dt);
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        public static DataTable ExecuteQuery(String spName)
        {
            DataTable dt = new DataTable();
            try
            {
                SqlConnection con = Connect_DB.ConnectionData();
                con.Open();
                SqlCommand command = con.CreateCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = spName;
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                adapter.Fill(dt);
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        ////////////////////////Thuc thi procedure co tham so truyen vao/////////////////////////////////
        public static int ExecuteNoneQuery(String spName, List<SqlParameter> listpara)
        {
            int n = 0;
            try
            {
                SqlConnection con =Connect_DB.ConnectionData();
                con.Open();
                SqlCommand command = con.CreateCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = spName;
                if (listpara != null)
                {
                    foreach (SqlParameter para in listpara)
                        command.Parameters.Add(para);
                }
                n = command.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return n;
        }


    }
}
